<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>class创建对象</title>
</head>

<body>
    <div id="app"></div>
</body>
<script>
    //6、使用class创建对象
    class Person {
        //私有化属性，只能在当前的类中可以使用
        #name = "tom";
        #age = 20;
        #gender = "gender";
      

        //构造方法,有且只能有一个,用于创建对象
        constructor(name, age, gender) {
            this.#age = age;
            this.#gender = gender;
            this.#name = name;
        }

        //返回对象的字符串形式的函数
        toString() {
            return `{"name":"${this.#name}","age":"${this.#age}","gender":"${this.#gender}"}`
        }

        get getName() {
            return this.#name
        }

        set setName(name) {
            this.#name = name
        }

        set setAge(age) {
            if (!age) {
                //手动自定义抛出异常对象，程序就会挂掉
                throw new Error("用户的年龄不能为空")
            }
            this.#age = age
        }

        get getAag() {
            return this.#age
        }

        work() {}
        study() {}
    }

    //创建对象
    let p = new Person('tom', 20, "男")
    console.log(p.toString())
    p.setName = "sam"
    console.log(p.toString())
    let age = 10
    p.setAge = age
    console.log(p.getAge)
</script>


</html>